<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/23
 * Time: 16:49
 */

namespace app\admin\model;


class GoldRoomsType extends DataBase
{

    protected $autoWriteTimestamp = true;

    public function getCreateTimeAttr($value){
        return date('Y-m-d H:i:s',$value);
    }


    public function indexData()
    {
        $page_config = array(
            'page' => input('offset', 1),
            'list_rows' => input('limit', 10),
        );
        $type = input('mode_type');
        $data = $this
            ->alias('a')
           // ->join('ss_gift_url b','a.gift_id = b.id','left')
            ->join('fw_gold_egg c','a.gold_egg_id  =c.id','left')
            ->field('a.*,c.egg_name')
            ->where('a.mode_type',$type)
            ->paginate($page_config);

        foreach($data as $key => $value){
            if($value['gift_type'] == 1 || $value['gift_type'] == 2){
//                $giftInfo  = Gift::where(['id'=>$value['gift_id']])->field('gift_name,diamonds')->find();
                $giftInfo  = Gift::where(['id'=>$value['gift_id']])->field('gift_name')->find();
                $data[$key]['gift_name'] = $giftInfo['gift_name'];
//                $data[$key]['diamonds'] = $giftInfo['diamonds'];
                $data[$key]['diamonds'] = 0;
            }elseif($value['gift_type'] == 3){
                $giftInfo  = Prop::where(['id'=>$value['gift_id']])->field('prop_name as gift_name')->find();
                $data[$key]['gift_name'] = $giftInfo['gift_name'];
                $data[$key]['diamonds'] = '道具无价值';
            }
        }
    //    halt($data);
        return $data;
    }
    //宝箱奖品添加
    public function  switchBoxAdd()
    {
        $newData = input('post.');
        $map['gift_id']=$newData['gift_id'];
        $map['gold_egg_id']=$newData['gold_egg_id'];
        $map['gift_type']=$newData['gift_type'];
        $map['mode_type']=$newData['mode_type'];
        $res = $this->where($map)->find();
        if($res)  return ['code'=>-100,'msg'=>'该礼物已存在'];
        $re = $this->save($newData);
       if($re) return ['code'=>100,'msg'=>'添加成功'];
        else return ['code'=>-100,'msg'=>'添加失败'];
    }

    public function switchBoxEdit()
    {
        $newData = input('get.');
        $res = $this->save($newData,[ 'id' => $newData['id'] ]);
        dailyLog(['content'=>'修改模式'.$newData['mode_type'].'宝箱'.$newData['gold_egg_id'].'库存id为'.$newData['id'].'默认库存改为'.$newData['default_stock'],'type'=>3]);
        if($res) return ['code'=>100,'msg'=>'修改成功'];
        else return ['code'=>-100,'msg'=>'修改失败'];
    }

    public function getGiftData($type,$mode_type,$gold_id)
    {
        if($type==2){
            $map[] = ['default_stock','=',0];
            $map[] = ['mode_type','=',$mode_type];
            $map[] = ['gold_egg_id','=',$gold_id];
            $data = $this->field('id,gift_id')->where($map)->select()->toArray();
        }
        if($type==1){
            $map[] = ['mode_type','=',$mode_type];
            $map[] = ['gold_egg_id','=',$gold_id];
            $data = $this->field('id,gift_id')->where($map)->select()->toArray();
        }
        foreach($data as $k=>$v){
            $giftInfo =  GiftUrl::where('id','=',$v['gift_id'])->field('gift_name,diamonds')->find();
            $data[$k]['gift_name'] = $giftInfo['gift_name'];
            $data[$k]['diamonds'] = $giftInfo['diamonds'];
        }
        return $data;
    }

    //轮次奖品信息
    public function loopGiftInfo()
    {
        $gift_type = input('gift_type');
        $gold_id = input('gold_id');
        $mode_type = input('mode_type');
        $stock_type = input('stock_type');
        if($stock_type ==1){
            $map[] = ['default_stock','=',0];
        }
        $map[] = ['mode_type','=',$mode_type];
        $map[] = ['gift_type','=',$gift_type];
        $map[] = ['gold_egg_id','=',$gold_id];
        $data = $this->field('id,gift_id')->where($map)->select()->toArray();
        if($gift_type == 1){
            foreach($data as $k=>$v){
                $giftInfo =  GiftUrl::where('id','=',$v['gift_id'])->field('gift_name,diamonds')->find();
                $data[$k]['name'] = $giftInfo['gift_name'];
            }
        }elseif ($gift_type == 3){
            foreach($data as $k=>$v){
                $giftInfo =  Prop::where('id','=',$v['gift_id'])->field('prop_name')->find();
                $data[$k]['name'] = $giftInfo['prop_name'];
            }
        }
        return ['code'=>100,'data'=>$data];
    }

    //指定用户的礼物刷选
    public function getGoldGift($redis){
        $prize_pool = json_decode($redis->get('prize_pool'),true);
        $gold_id = input('gold_id');

        $max_diamonds = $prize_pool[$gold_id] ? $prize_pool[$gold_id] : 0;

        $gift_type = input('gift_type');
        $mode_type = input('mode_type');

        $map[] = ['a.gold_egg_id','eq',$gold_id];
        $map[] = ['a.mode_type','eq',$mode_type];
        $map[] = ['b.diamonds','elt',$max_diamonds];

        $data = [];
        if($gift_type == 1){
            $data = $this
                ->alias('a')
                ->field('a.id,b.gift_name as name,b.diamonds')
                ->join('ss_gift_url b','a.gift_id = b.id','left')
                ->where($map)
                ->select()
                ->toArray();
        }elseif ($gift_type == 3){
            $data = $this
                ->alias('a')
                ->field('a.id,b.prop_name as name,b.diamonds')
                ->join('ss_prop b','a.gift_id = b.id','left')
                ->where($map)
                ->select()
                ->toArray();
        }
        return ['code'=>100,'data'=>$data];
    }

    //奖池概率
    public function prizePool()
    {
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis->auth('susu123');
        $gold_id = input('gold_id');
        $mode_type = input('mode_type');
        $least_time = $redis->get('least_time'.$mode_type.$gold_id);  //保存最小公倍数
        $least_time  = $least_time ? $least_time : 1;

        $map[] = ['a.gold_egg_id','eq',$gold_id];
        $map[] = ['a.mode_type','eq',$mode_type];
        $map[] = ['a.gift_type','eq',1];
        //总库存
        $egg_info = GoldEgg::where('id',$gold_id)->find();
        $one_stock = $this->where(['gold_egg_id'=>$gold_id,'mode_type'=>$mode_type])->sum('default_stock');
        //礼物总价值
        $gift_price = $this
            ->alias('a')
            ->field('SUM(a.default_stock * b.diamonds) as price')
            ->join('ss_gift_url b','a.gift_id = b.id','left')
            ->where($map)
            ->select();
        $gift_price = $gift_price[0]['price'];
        //道具总价值
        $map[2] = ['a.gift_type','eq',3];
        $prop_price = $this
            ->alias('a')
            ->field('SUM(a.default_stock * b.diamonds) as price')
            ->join('ss_prop b','a.gift_id = b.id','left')
            ->where($map)
            ->select();
        $prop_price = $prop_price[0]['price'] ? $prop_price[0]['price'] : 0;
        //单轮比例
        $s_rate = round(($gift_price + $prop_price)/($one_stock * $egg_info['egg_price']),5);

        $num = 0;
        //实际价值
        $real_price = 0;
        //实际比例
        $r_rate  = 0;
        $custom_loop = json_decode($redis->get('custom_loop_add_'.$mode_type.$gold_id),true);
        if(!empty($custom_loop)){
            foreach($custom_loop as $k => $v){
                $prize_info = $this->where('id','=',$k)->field('gift_id,gift_type')->find();
                if($prize_info['gift_type'] == 1) {
                    $diamonds = GiftUrl::where('id', '=', $prize_info['gift_id'])->value('diamonds');
                }elseif ($prize_info['gift_type'] == 3){
                    $diamonds = \app\admin\model\Prop::where('id', '=', $prize_info['gift_id'])->value('diamonds');
                }
                $num += $least_time/$v;
                $real_price += $diamonds * floor($least_time/$v);  //指定轮次的总价值
            }
            $real_price += ($gift_price + $prop_price) * $least_time;  //总价值
            $total_stock = $one_stock * $least_time + $num;
            $r_rate = $real_price / ($total_stock * $egg_info['egg_price']);
        }
        $r_rate = $r_rate ? $r_rate : $s_rate;
        $prize_pool = unserialize($egg_info['prize_pool']);
        $p_pool = $prize_pool[$mode_type] ? $prize_pool[$mode_type] : '未设置';

        return ['s_rate'=>$s_rate,'r_rate'=>$r_rate,'p_pool'=>$p_pool,'least_time'=>$least_time];
    }
}